package meta.common.msg;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import meta.common.GlobalConst;
import meta.common.log.CmnLogLevelManager;
import meta.common.prop.CmnPropertiesUtils;

public class MsgResourceManager {

    private static ResourceBundle messages;

    static {
        initResource();
    }

    private static void initResource() {
//        String path = CmnPropertiesUtils.getMsgResFile();

        try (InputStream is = CmnLogLevelManager.class.getClassLoader().getResourceAsStream("msgResource.properties")) {
            messages = new PropertyResourceBundle(
                    new InputStreamReader(
                            is,
                            GlobalConst.SYS_FILE_ENCODING));
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }

    public static String getMessage(String messageCode) {
        return getMessage(messageCode, null);
    }

    public static String getMessage(String messageCode, Object[] arguments) {
        String message = null;

        try {
            message = messages.getString(messageCode);
        } catch (MissingResourceException e) {
            return "";
        }

        if (arguments != null) {
            try {
                message = MessageFormat.format(message, arguments);
            } catch (IllegalArgumentException e) {
                return "";
            }
        }

        return message;
    }
}
